package com.sugar.follow.up.module.business.service.followupquestionrecord;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.sugar.follow.up.module.business.controller.admin.followupquestionrecord.vo.*;
import com.sugar.follow.up.module.business.dal.dataobject.followupquestionrecord.FollowUpQuestionRecordDO;
import com.sugar.follow.up.framework.common.pojo.PageResult;
import com.sugar.follow.up.framework.common.pojo.PageParam;
import com.sugar.follow.up.framework.common.util.object.BeanUtils;

import com.sugar.follow.up.module.business.dal.mysql.followupquestionrecord.FollowUpQuestionRecordMapper;

import static com.sugar.follow.up.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.follow.up.framework.common.util.collection.CollectionUtils.convertList;
import static com.sugar.follow.up.framework.common.util.collection.CollectionUtils.diffList;
import static com.sugar.follow.up.module.business.enums.ErrorCodeConstants.*;

/**
 * 随访发起答题记录 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class FollowUpQuestionRecordServiceImpl implements FollowUpQuestionRecordService {

    @Resource
    private FollowUpQuestionRecordMapper followUpQuestionRecordMapper;

    @Override
    public Long createFollowUpQuestionRecord(FollowUpQuestionRecordSaveReqVO createReqVO) {
        // 插入
        FollowUpQuestionRecordDO followUpQuestionRecord = BeanUtils.toBean(createReqVO, FollowUpQuestionRecordDO.class);
        followUpQuestionRecordMapper.insert(followUpQuestionRecord);

        // 返回
        return followUpQuestionRecord.getId();
    }

    @Override
    public void updateFollowUpQuestionRecord(FollowUpQuestionRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateFollowUpQuestionRecordExists(updateReqVO.getId());
        // 更新
        FollowUpQuestionRecordDO updateObj = BeanUtils.toBean(updateReqVO, FollowUpQuestionRecordDO.class);
        followUpQuestionRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteFollowUpQuestionRecord(Long id) {
        // 校验存在
        validateFollowUpQuestionRecordExists(id);
        // 删除
        followUpQuestionRecordMapper.deleteById(id);
    }

    @Override
        public void deleteFollowUpQuestionRecordListByIds(List<Long> ids) {
        // 删除
        followUpQuestionRecordMapper.deleteByIds(ids);
        }


    private void validateFollowUpQuestionRecordExists(Long id) {
        if (followUpQuestionRecordMapper.selectById(id) == null) {
            throw exception(FOLLOW_UP_QUESTION_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public FollowUpQuestionRecordDO getFollowUpQuestionRecord(Long id) {
        return followUpQuestionRecordMapper.selectById(id);
    }

    @Override
    public PageResult<FollowUpQuestionRecordDO> getFollowUpQuestionRecordPage(FollowUpQuestionRecordPageReqVO pageReqVO) {
        return followUpQuestionRecordMapper.selectPage(pageReqVO);
    }

}